import { Application } from "express";

export function autojsExpress(app: Application) {
    app.use('/docs', (req, res) => {
        res.type(`html`).end(`<html><head></head><body></body></html>`)
    });

    app.use(`/docs/documentation.json`, (req, res) => {
        res.type(`html`).end(`<html><head></head><body></body></html>`)
    })

    app.all('/api/v1/plugins', (req, res) => {
        const plugins = [
            {
                package_name: 'org.autojs.plugin.ffmpeg',
                name: '官方FFMpeg插件',
                version: '1.1',
                version_code: 1,
                summary: "FFmpeg是一套可以用来记录、转换数字音频、视频，并能将其转化为流的开源计算机程序。本插件用于利用ffmpeg处理音视频文件，比如从格式转换等。",
                icon: 'https://www.wuyunai.com/docs/assets/image/ffmpeg-plugin.png',
                url: 'https://www.wuyunai.com/docs/blog/ffmpeg-plugin.html',
                installed: false,
                update_timestamp: 0
            },
            {
                package_name: 'org.autojs.plugin.mlkit',
                name: '官方MLKitOCR插件',
                version: '1.1',
                version_code: 1,
                summary: "FFmpeg是一套可以用来记录、转换数字音频、视频，并能将其转化为流的开源计算机程序。本插件用于利用ffmpeg处理音视频文件，比如从格式转换等。",
                icon: 'https://www.wuyunai.com/docs/assets/image/mlkit-ocr-plugin.png',
                url: 'https://www.wuyunai.com/docs/blog/mlkit-ocr-plugin.html',
                installed: false,
                update_timestamp: 0
            },
            {
                package_name: 'cn.lzx284.p7zip',
                name: '7Zip通用压缩插件',
                version: '1.2.1',
                version_code: 4,
                summary: '本插件基于p7zip 16.02制作，支持多种格式文件的压缩与解压。7-Zip是一款完全免费而且开源的压缩软件，相比其他软件有更高的压缩比但同时耗费的资源也相对更多，能提供比使用 PKZip 及 WinZip 高2~10%的压缩比率。',
                icon: 'https://www.wuyunai.com/docs/assets/image/7zip-plugin.png',
                url: 'https://www.wuyunai.com/docs/blog/7zip-plugin.html',
                documentation_url: 'https://www.wuyunai.com/docs/blog/7zip-plugin.html',
                installed: false,
                update_timestamp: 0
            },
            {
                package_name: 'com.hraps.pytorch',
                name: 'Pytorch插件',
                version: '1.0.0',
                version_code: 1,
                summary: 'Pytorch模块提供了已完成的深度学习神经网络模型在安卓设备上执行的功能，可以实现常规程序难以实现的功能，如：图像识别，语言翻译，语言问答等。',
                icon: 'https://www.wuyunai.com/docs/assets/image/pytorch-logo.png',
                url: 'https://www.wuyunai.com/docs/v8/thirdPartyPlugins.html',
                documentation_url: 'https://www.wuyunai.com/docs/v8/thirdPartyPlugins.html#pytorch插件',
                installed: false,
                update_timestamp: 0
            }
        ];
        res.json(plugins);
    });

    app.get('/api/v1/account', function (req, res) {
        var response = {
            "id": "6131f76468e4553fba39ae4c",
            "now": Date.now(),
            "emailAddress": "QQ：1037483576",
            "fullName": "微信：imeepos",
            "paidServices": {
                "v8": {
                    "expires": 3207475409760
                }
            },
            "permissions": {}
        };
        res.set({
            "Server": "nginx/1.12.2",
            "Date": new Date().toUTCString(),
            "Content-Type": "application/json; charset=utf-8",
            "X-Powered-By": "Sails <sailsjs.com>",
            "ETag": 'W/"b1-VocwenWVuO/o20Vz9VD1X4Qvte4"',
            "Cache-Control": "no-cache, no-store",
            "Connection": "keep-alive",
            "Keep-Alive": "timeout=5",
        });

        res.status(200).json(response);
    });

    app.get(`/api/v1/announcements`, (req, res) => {
        res.json({})
    })

    app.post(`/api/v8/security/validation2`, (req, res) => {
        const body = req.body;
        res.json({
            "data": "hfEo9RELZhFBarFnpEeSXLO1P3A+/YlrvgrcdHOcSsiC9RWX1CYRn9bH8GIn4Cq5Y8azZmRzRxXI9DiZTolC3aQ9BSjeHOt41kQrW5D/Ue3lVPnvIjCI1yAEFNcB/uzeldp4uvcybqkl5H04Op0HJeezFAATyUF9KWbz2jxiKA2fs6CTPwjoe30Z1mv8qrMSR7YWQjhpvoGELXQtd+J+KeR79wGC0Z2CNN008UcqKph/uourYwD28dkN4Rb5sVCFKn5ACD9BZyXvmnuXgh/yg1WtfcAAhSB5AzyU4578DSAB4eYwjWdst8p6HK5+zwkGqOpVhBvbPHWIkYje6hqIDU85ojNIdAmE7aCPyT1Cm3WTiVeXxeeG6E0SWUj7FGdOheEm7Ed38Wi/tzc3vEtgo+bpCTK87F9phBm1/O4u8aeBevd9XTXaKqnA4OIZSrlTQq3Ep8Pw7vSkrvTZo6W3USXSkHtPBG0KhDDLLDZdAf2Z/bLKY4DTNfhYZM8MTgA9HQ2BP0ToqCzebGVgxHLo2FSYpfDSQd2Dd2VSDo0NRSS9hTWG2xL/PdAGV1gMIXnPOxl96OmEbAuJqnjNeJDlELsi4wGEHOTwPbcO/5ZmEgrJVszStVAdyv1u984Sj/rXu7fRS4r6NGGUDHBbzmfR4rh3P5C3ALUEdjY1crQ3nOA="
        })
    })

    app.get('/static/legal/version.json', function (req, res) {
        // 提取 x-csrf-token 头部信息
        var csrfToken = req.headers['x-csrf-token'];
        var response = {
            "version": 20230211,
            "wording": "为了给您提供更好的服务，我们更新了“服务协议”和“隐私政策”，特别提示您重新阅读并充分理解“服务协议”和“隐私政策”各条款。\n您可以阅读%s和%s全文了解详细信息。如您同意并接受更新后的“服务协议”和“隐私政策”各条款，请点击“同意”继续接受我们的服务。"
        };
        res.set({
            "Server": "nginx/1.12.2",
            "Date": new Date().toUTCString(),
            "Content-Type": "application/json; charset=utf-8",
            "X-Powered-By": "Sails <sailsjs.com>",
            "ETag": 'W/"b1-VocwenWVuO/o20Vz9VD1X4Qvte4"',
            "Cache-Control": "no-cache, no-store",
            "Connection": "keep-alive",
            "Keep-Alive": "timeout=5"
        });
        res.status(200).json(response);
    });

    app.post('/api/v1/security/validation2', function (req, res) {

        // 提取 x-csrf-token 头部信息
        var csrfToken = req.headers['x-csrf-token'];
        var response;
        if (csrfToken != "Tbs6hIVo--Ngb_G9VJ3lnoMR1EYRnQli5bEY") {
            response = {
                "data": "uNl8AK0WM6mIAQAAM9bHGgAAAACaX4kztI8jdDdMKBwYbba4oNAKCHba0nRgN7zXoP0IzjEyM2NjZjgzMmFiZTg5OGYAAQAAAAAAAAAyWsXfnWpHYVlJ4ZPT/u3n+ZH3NLvubrTRJnas08r0ijocgKnKqCxTFvJgeZnWx2omp6CzeSFWEG8aEaarJ4XMkp9+F8sdy2yFkqkOrp41KmCfShbIQX4hCYeD0mVOOwfOVLpQLJjg18FvFvHm9TKYzK5ysfv9UHuHn8+dexgnLM28j5BDrIFv9B9XS+UW1x/lLAwe+QzBEAWzsYFKPkVJ9Mc0L5lG/i8Eh7bxcGHIg1L+VbC4t9+CZXcF6DOoy75I40omuQs/gtbLCsMEr7fdsiDQ76iukr1SwLHVIEaXrNutrvvqKp+UBcq4WGQEM+aMj46S3pd7+h17J8vKdTVknI2IOJPZM2mVjGCQ3MBriG5HQqghbFE3y/VEPWpmtkgjDXqc09vuYA4PLxnV1AbvoAEvy8FgqxY00MXANK2MMixzZorUIC2Jk1hBLgPYHd1lMPlAMt8Deab3KZ0sJNLMo/7tAzk50DrPse3onAg5oA5QTSDfKBI2AtZP+DmPYrtsa96iUFK9iz8/18Pnhw/GBd+ceDR00dpQRVGjqTFxftAtZFr9kFYXTfz94+uq/fnVlH4eDGQiNAvuPg/4nQLXlde3lDYp5loaN2MkjL4uK9m8uQjH68217L195jsXANSo8IKjJYqWzcA1oCF/Smnmwc03k0Uk5OcfunIF/AGJ1g=="
            };
        } else {
            response = {
                "data": "XFSLAJob6izJAzdNz4d7zvnjSYFK3DiLgFMUaZZp4uWAdvyxAH7AeN152Gf8Qpoofi9+DLdMaVhgKTvvRwV8qd2IjybaGw4y6qZRqrktuMC0+jzG1ZGndkV3pAD7J7kdEZYeA62vZGZLyAotpgKArQrOkSi0LWy2ll/f6c99hb4aIiHyNAC+K8t4SzFiGjw7BGojPEm4VdIwOOKGwGN+cCBdAPFTtxz+RpYT4d4oiOa4287wKfJKqK2HNPz7+d3ctyS1blJXpf0MGEI+kZc/sWAKduyZgCfu7n0GA85Bqj4cl9z2TsZeUD3MG2Sr5+mzgbDmxJc9FSGdw1bpn3PT1Z1t5NsCy/fR3csBrsEQgqX0w6j10D29p0WxrM5Irk0BbehR53GLPvoROXVTFIdGjk7+mJIFvTvGIbq9lsgPABHR8MLsnrJZDwVUQ4feXuQDCROmXTeOJ6ixwqzsPjW4BEAmH3kg1DVF8IWyXPQg3t52voiPUzm/k1/XdAcUeOrmwi5H2g9L8chjK9sDLIzYUCUKmaLf2XxtM5RVL0QnlZQ/7EdZJ6yPD7RnFwN3Fiuwq4e9xTOqyzuu/S+bYfw0dM+c4hgNCTUnN8IWZMK0eApXXgP1zUlIBHWfPOdpor+Ajmbln2QRny8Gqfm3lB4SydB+wnA1TL/HyxFgwHyKtJ9Eb1Ql"
            };
        }


        res.set({
            "Server": "nginx/1.12.2",
            "Date": new Date().toUTCString(),
            "Content-Type": "application/json; charset=utf-8",
            "X-Powered-By": "Sails <sailsjs.com>",
            "ETag": 'W/"b1-VocwenWVuO/o20Vz9VD1X4Qvte4"',
            "Cache-Control": "no-cache, no-store",
            "Connection": "keep-alive",
            "Keep-Alive": "timeout=5",
            "Transfer-Encoding": "chunked",
        });

        res.status(200).json(response);
    });

    app.get('/api/v1/config', function (req, res) {
        var response = {
            "wl": "0a4fd5d5accf385b8d5f382d7abcfea7"
        };
        res.set({
            "Server": "nginx/1.12.2",
            "Date": new Date().toUTCString(),
            "Content-Type": "application/json; charset=utf-8",
            "X-Powered-By": "Sails <sailsjs.com>",
            "ETag": 'W/"b1-VocwenWVuO/o20Vz9VD1X4Qvte4"',
            "Cache-Control": "no-cache, no-store",
            "Connection": "keep-alive",
            "Keep-Alive": "timeout=5"
        });

        res.status(200).json(response);
    });

    app.get('/csrfToken', function (req, res) {
        var response = {
            "_csrf": "Tbs6hIVo--Ngb_G9VJ3lnoMR1EYRnQli5bEY"
        };

        // 计算过期时间，当前时间加一个月
        var expiryDate = new Date();
        expiryDate.setMonth(expiryDate.getMonth() + 1);

        res.set({
            "Server": "nginx/1.12.2",
            "Date": new Date().toUTCString(),
            "Content-Type": "application/json; charset=utf-8",
            "X-Powered-By": "Sails <sailsjs.com>",
            "ETag": 'W/"b1-VocwenWVuO/o20Vz9VD1X4Qvte4"',
            "Cache-Control": "no-cache, no-store",
            "Connection": "keep-alive",
            "Keep-Alive": "timeout=5",
            "Set-Cookie": "sails.sid=s%3AZiwwINUFknF0pT7ncljTzWh-w1e-q2Wr.WYipgZnGJb2RubVHpzJlpjyuP9%2FPwCMoE8FIPP1sQGc; Path=/; Expires=" + expiryDate.toUTCString() + "; HttpOnly; Secure"
        });

        res.status(200).json(response);
    });
}